// 线段切分
import { _k, _polar, _rotate } from '../index.js'

// 圆弧切分 
export const polarPoints = (options = {}) => {

    let {
        o = [0, 0],
        r = 100,
        n = 6,
        a = 0,
        a2
    } = options

    let a1 = a
    a2 = a2 || (360 + a1)

    return Array.from({
        length: n
    }, (_, i) => {
        let a = a1 + i * (a2 - a1) / n
        return _polar(o, r, a)
    })
}

// 坐标网格线
export const polarGridPoints = (options) => {
    let {
        o = [0, 0],
        r = 100,
        n = 6,
        a = 0,
        m = 5
    } = options
    let a1 = a, a2 = 360 + a1
    return Array.from({ length: m }, (_, j) => {
        return Array.from({
            length: n
        }, (_, i) => {
            let a = a1 + i * (a2 - a1) / n
            return _polar(o, r * (j + 1), a)
        })
    })
}

// 等角
export const isometricPoints = (options = {}) => {
    let {
        o = [0, 0],
        r = 100,
        a = 45,
        n = 10
    } = options

    return Array.from({
        length: n
    }, (_, i) => {
        return _polar(o, r, a * i)
    })
}

// 等边
export const polygonPoints = (options = {}) => {
    let {
        o = [0, 0],
        r = 100,
        a = 45,
        n = 10,
        end,
        sweepFlag = true
    } = options

    let start = o
    end = end || _polar(o, r, a)
    // 内角
    let ia = 180 - 360 / n

    let points = [start, end]
    let fn = (p, o) => {
        let next = _rotate(p, o, ia * (sweepFlag ? 1 : -1)).map(t => _k(t))
        points.push(next)
        let len = points.length
        if (len < n) {
            fn(o, next)
        }
    }

    fn(start, end)
    return points


    // return Array.from({
    //     length: n
    // }, (_, i) => {
    //     return _polar(o, r, a * i)
    // })
}


    // let points = []
    // for (let i = 0; i < n; i++) {
    //     // a = i * 2 * Math.PI / n + (a1 / 2 * Math.PI) //等角
    //     // points[i] = [o[0] + r * Math.cos(a), o[1] + r * Math.sin(a)]
    //     let a = a1 + i * (a2 - a1) / n
    //     points[i] = _polar(o, r, a)
    //     // points[points.length] = [o[0] + r * _.cos(a), o[1] + r * _.sin(a)]
    //     // let r2 = r + 0.5 * r * _.sin(phi)
    //     // points[i] = _.polar(o, r2, a)
    //     // phi += 360 / n
    // }